package git.bankview.wl.report;

import java.sql.Connection;
import java.sql.PreparedStatement;

import git.bankview.sl.db.DBConnectionFactory;
import laya.semi.process.IGenericClassActivity;
import laya.semi.process.IProcessContext;

import org.apache.log4j.Logger;

public class ReportTransAuthSetter
	implements IGenericClassActivity
{
	private static Logger log = Logger.getLogger(ReportTransAuthSetter.class);

	private static String SQL_ROLETRANSINSERT = 
		"INSERT INTO roletrans (roleid, transid, methodid, statuscode) VALUES (?, ?, ?, ?)";
	
	public void execute(IProcessContext oCtx)
		throws Exception
	{
		String oTransid = (String)oCtx.getField("transid");
		
		Connection oConn = DBConnectionFactory.getCoreConnection();
		try
		{
			log.debug(SQL_ROLETRANSINSERT);
			PreparedStatement ps = oConn.prepareStatement(SQL_ROLETRANSINSERT);
			
			addAuths(ps, oCtx, new String[] {"roleids", "rolemethodids", "rolestatuscodes"}, oTransid);
			addAuths(ps, oCtx, new String[] {"userids", "usermethodids", "userstatuscodes"}, oTransid);
			
			ps.executeBatch();
		}
		finally
		{
			oConn.close();
		}
	}
	
	private void addAuths(PreparedStatement ps, IProcessContext oCtx, String[] vFieldName, String transId)
		throws Exception
	{
		String[] vId = (String[])oCtx.getField(vFieldName[0]);
		Integer[] vMethod = (Integer[])oCtx.getField(vFieldName[1]);
		Integer[] vCode = (Integer[])oCtx.getField(vFieldName[2]);
		
		for (int i = 0; i < vId.length; ++i)
		{
			log.debug(transId + " --------> " + i);
			log.debug(vId[i] + "." + transId + "." + vMethod[i] + "." + vCode[i]);
			ps.setString(1, vId[i]);
			ps.setString(2, transId);
			ps.setInt(3, vMethod[i].intValue());
			ps.setInt(4, vCode[i].intValue());
			ps.addBatch();
		}
	}
}
